

import sys
project_root = r'E:\pycharm_place\lucky_git_42\meiduo_mall'
sys.path.insert(0, project_root)

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings")


import django
django.setup()

from apps.goods.models import SKU
from uitls.goods import get_breadcrumb
from uitls.goods import get_goods_specs
from uitls.goods import get_categories
def generic_detail_html(sku):
    # try:
    #     sku = SKU.objects.get(id=sku_id)
    # except SKU.DoesNotExist:
    #     pass
    #     1.分类数据
    categories = get_categories()
    #     2.面包屑
    breadcrumb = get_breadcrumb(sku.category)
    #     3.SKU信息
    #     4.规格信息
    goods_specs = get_goods_specs(sku)

    context={
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs':goods_specs
    }
    #1.加载渲染的模板
    from django.template import loader
    detail_template = loader.get_template('detail.html')
    #2.把数据给模板
    detail_html_data=detail_template.render(context)
    #3.把渲染好的HTML，写入到指定文件
    # "E:\pycharm_place\lucky_git_42\front_end_pc\js"
    file_path = r'E:\pycharm_place\lucky_git_42\front_end_pc\goods\%s.html'%sku.id
    with open(file_path,'w',encoding='utf-8') as f:
        f.write(detail_html_data)
    print(sku.id)

skus=SKU.objects.all()
for sku in skus:
    generic_detail_html(sku)

"""
详情页面与首页不同
详情页面的内容变化的比较少，一般也就是改变商品的价格

1.详情页面 应该在上线的时候 统一生成一遍
2.应该是运营人员修改的时候生成（定时任务）
"""